package com.paperobot.common.util;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
	private int totalRows;  // 总行数即总记录数 
    private int totalPages; // 总页数 
    private int currentPage = 1; // 当前页 
    private int pageSize = 15; // 每页存放3条记录 
    private int firstRow = 0; // 数据库中第一行数据 
	
    public PageUtil() {
		super();
	}
    
    // 根据请求参数pageType翻页,即设置firstRow 
    // 根据请求参数rows总记录数计算总页数 
    @SuppressWarnings("unchecked")
	public List getCurrentPageRows(String pageType,int totalRows, List allRowsList) { 
        this.totalRows = totalRows; 
        this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize; 
        pageType = pageType.toLowerCase(); 
        if ("first".equals(pageType)) { 
            first(); 
        } else if ("last".equals(pageType)) { 
            last(); 
        } else if ("next".equals(pageType)) { 
            next(); 
        } else if ("previous".equals(pageType)){ 
            previous(); 
        } 
        
//        System.out.println("firstRow: " + firstRow + ", currentPage: " + currentPage + ", totalPages: " +totalPages+ ", totalRows: "+totalRows);
        
        List currentRowsList = new ArrayList();
		int endRow = firstRow + pageSize;
		if(endRow > totalRows){
			endRow = totalRows;
		}
		
		for(int index = firstRow; index < endRow; index++){
			currentRowsList.add(allRowsList.get(index));
		}

		return currentRowsList;
    } 
    	
    private void first() { 
        currentPage = 1; 
        firstRow = 0; 
    } 

    private void previous() { 
        if (totalPages == 0) { 
            currentPage = 1; 
        } else { 
            if (currentPage == 1) { 
                currentPage = totalPages; 
            } else { 
            	--currentPage; 
            } 
        } 
        firstRow = (currentPage - 1) * pageSize; 
    } 

    private void next() { 
        if (totalPages == 0){ 
            currentPage = 1; 
        } else { 
            if (currentPage < totalPages) { 
            	++currentPage; 
            }else if (currentPage == totalPages) { 
                currentPage = 1; 
            } 
        } 
        firstRow = (currentPage - 1) * pageSize; 
    } 

    private void last() { 
        currentPage = totalPages; 
        if (totalPages == 0) { 
            currentPage = 1; 
        } 
        firstRow = (currentPage - 1) * pageSize; 
    } 
    
    public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getFirstRow() {
		return firstRow;
	}
	public void setFirstRow(int firstRow) {
		this.firstRow = firstRow;
	}
	
}
